package com.foreknow.elm.dao.impl;


import com.foreknow.elm.dao.OrderdetailetDao;
import com.foreknow.elm.dao.OrdersDao;
import com.foreknow.elm.po.Business;
import com.foreknow.elm.po.OrderDetaiLet;
import com.foreknow.elm.po.Orders;
import com.foreknow.elm.util.CommonUtil;
import com.foreknow.elm.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class OrdersDaoImpl implements OrdersDao {
    private Connection con;
    private PreparedStatement pst;
    private ResultSet rs;

    @Override
    public Integer selectByOrdersId(String userId, Integer businessId, Integer daId, Double orderTotal) throws SQLException {
        int result = 0;
        String date = CommonUtil.getCurrentDate();
        String sql = "insert into orders(userId,businessId,daId,orderTotal,orderDate) values(?,?,?,?,?)";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
            pst.setString(1, userId);
            pst.setInt(2, businessId);
            pst.setInt(3, daId);
            pst.setDouble(4, orderTotal);
            pst.setString(5, date);
            int i = pst.executeUpdate();
            rs = pst.getGeneratedKeys();
            while (rs.next()) {
                result = rs.getInt(1);
            }
        } finally {
            DBUtil.close(pst);
        }
        return result;
    }

    @Override
    public Orders getOrdersById(Integer orderId) throws SQLException {
        Orders orders = null;
        String sql = "select * from orders o,business b where o.businessId=b.businessId and o.orderId=? ";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setInt(1, orderId);
            rs = pst.executeQuery();
            while (rs.next()) {
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                orders.setBusiness(business);

            }
        } finally {
            DBUtil.close(pst);
        }
        return orders;
    }

    @Override
    public List<Orders> listOrdersByUserId(String userId) throws SQLException {
        List<Orders> list = new ArrayList<>();
        String sql = "select * from orders o,business b where o.businessId=b.businessId and o.userId=?";
        try {
            con = DBUtil.getConnection();
            pst = con.prepareStatement(sql);
            pst.setString(1, userId);
            rs = pst.executeQuery();
            while (rs.next()) {
                Business business = new Business();
                business.setBusinessId(rs.getInt("businessId"));
                business.setBusinessName(rs.getString("businessName"));
                business.setBusinessAddress(rs.getString("businessAddress"));
                business.setBusinessExplain(rs.getString("businessExplain"));
                business.setBusinessImg(rs.getString("businessImg"));
                business.setOrderTypeId(rs.getInt("orderTypeId"));
                business.setStarPrice(rs.getDouble("starPrice"));
                business.setDeliveryPrice(rs.getDouble("deliveryPrice"));
                business.setRemarks(rs.getString("remarks"));
                Orders orders = new Orders();
                orders.setOrderId(rs.getInt("orderId"));
                orders.setUserId(rs.getString("userId"));
                orders.setBusinessId(rs.getInt("businessId"));
                orders.setOrderDate(rs.getString("orderDate"));
                orders.setOrderTotal(rs.getDouble("orderTotal"));
                orders.setDaId(rs.getInt("daId"));
                orders.setOrderState(rs.getInt("orderState"));
                orders.setBusiness(business);
                list.add(orders);
            }
        } finally {
            DBUtil.close(pst);
        }

        return list;
    }

    @Override
    public Integer updateState(Integer orderId) throws SQLException {
        int result=0;
        String sql="update orders set orderState=1 where orderId=?";
        try {
            con=DBUtil.getConnection();
            pst=con.prepareStatement(sql);
            pst.setInt(1,orderId);
            result = pst.executeUpdate();
        }finally {
            DBUtil.close(pst);
        }

        return result;
    }
}



